package com.qumitech.visualnovel.service.version.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dto.VersionDTO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.version.VersionCfgService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: yilu
 * @Date: 2019/1/15 12:07
 * @Description:
 */

@Service
public class VersionCfgServiceImpl implements VersionCfgService {

    private final String VERSION_URL = "/app/version";
    private final String VERSION_GET = "/admin/version/all";


    @Override
    public Map<String, Object> addVersion(VersionDTO versionDTO) {
        String url = UrlConstant.SERVER_URL + VERSION_URL;
        String req = JSONObject.toJSONString(versionDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return null;
    }

    @Override
    public Map<String, Object> deleteVersion(VersionDTO dto) {
        String url = UrlConstant.SERVER_URL + VERSION_URL ;
        Map<String, String> headMap = new HashMap<>();
        boolean isParamNull = dto.getVersion() == null || dto.getPhoneType() == null;
        if(isParamNull){
            throw new BusinessException(ResultEnum.PARA_ERR);
        }
        headMap.put("version", dto.getVersion());
        headMap.put("app_package", dto.getAppPackage());
        headMap.put("phone_type", dto.getPhoneType().toString());
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return null;
    }

    @Override
    public Map<String, Object> updateVersion(VersionDTO versionDTO) {
        String url = UrlConstant.SERVER_URL + VERSION_URL;
        String req = JSONObject.toJSONString(versionDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return null;
    }

    @Override
    public Object getVersion(Integer limit, Integer offset, Byte phoneType) {
        String url = UrlConstant.SERVER_URL + VERSION_GET;
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("limit", limit.toString());
        paramMap.put("offset", offset.toString());
        if(phoneType == null){
            paramMap.put("phone_type", null);
        }else {
            paramMap.put("phone_type", phoneType.toString());
        }
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        return rsp.getData();
    }
}
